package com.jiang.action;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jiang.service.INewsService;
import com.jiang.vo.News;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
@Controller
@Scope(value="prototype") // 配置多例线程安全
@ParentPackage("root")
@Namespace("/pages/news")
@Action(value="NewsAction")
public class NewsAction extends ActionSupport {

	// 要调用的业务层的Service类注入
	@Autowired
	private INewsService newsService;
	// 依靠前端传入参数绑定(依靠反射)，这个就比SpringMVC差一些能力
	private News news;
	
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	
	public void insertNews() {
		
		System.out.println("拦截器进行数据验证!");
		newsService.insert(news);
		
	}
	
	public void updateNews(){
		System.out.println("拦截器进行数据验证!");
		newsService.update(news);
	}
	
	/**
	 * 前端传来一堆|分隔的数据要删除
	 */
	public void deleteNews(){
		System.out.println("拦截器进行数据验证!");
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids_str = request.getParameter("ids");
		// 字符串分割
		String[] ids = ids_str.split("\\-");
		Set<Integer> ids_set = new HashSet<>();
		for(String id: ids){
			ids_set.add(Integer.parseInt(id));
		}
		newsService.delete(ids_set);
	}
	
	public void get(){
		System.out.println("拦截器进行数据验证!");
		System.out.println(newsService.get(news.getNid()));
	}
	
	public void findAll(){
		System.out.println("拦截器进行数据验证!");
		System.out.println(newsService.list());
	}
	
	public void findAllSplit(){
		System.out.println("拦截器进行数据验证!");
		System.out.println(newsService.list("title", "", 1, 10));
	}
	
}
